﻿using System;
using Cysharp.Threading.Tasks;
using Internal.KuiHuaBaoDian.Services.AD;
using KuiHuaBaoDian.Services.AD;

namespace KuiHuaBaoDian.Services.AD {

    public interface IADService {

        public interface IData {

            object Value { get; }
        }

        UniTask<bool> Emit(IData data, bool requireTimestamp = false);
    }

    public static class ADService {

        /// <summary>
        /// 单例
        /// </summary>
        public static IADService Instance => m_Instance ??= KHBD.TypeMap.Instantiate<IADService>();
        private static IADService m_Instance;
    }

    [Implement(typeof(IADService))]
    internal sealed class KHBD_ADService : IService, IADService, IADServiceInternal {

        void IService.OnApplicationQuit() {
        }

        private Func<IADService.IData, bool, UniTask<bool>> m_OnOnce;

        void IADServiceInternal.Initialize(Func<IADService.IData, bool, UniTask<bool>> onOnce) {
            m_OnOnce = onOnce;
        }

        public async UniTask<bool> Emit(IADService.IData data, bool requireTimestamp = false) {
            await UniTask.Yield();
            if (m_OnOnce != null) {
                return await m_OnOnce.Invoke(data, requireTimestamp);
            }
            return true;
        }
    }
}

namespace Internal.KuiHuaBaoDian.Services.AD {

    public interface IADServiceInternal {

        void Initialize(Func<IADService.IData, bool, UniTask<bool>> onOnce);
    }
}
